import React from 'react';
import {connect} from 'dva';
import {Link, browserHistory} from 'dva/router';
import {Table, Row, Col, Form, Input, Icon, InputNumber, Radio, DatePicker, Select, Upload, Button, Switch, Modal, Popconfirm, Tabs, message} from 'antd';
import moment from 'moment'
import OperatorConstant from "../../../utils/OperatorConstant";
import Deposit from '../../../components/capital/Deposit'
import RightContacts from "../../../utils/RightConstants";
import RightBox from "../../../components/commons/RightBox";
const R = require('ramda');
const dateFormat = 'YYYY-MM-DD';
const { RangePicker } = DatePicker;
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;

export default connect(({deposit}) => ({deposit}))(
    Form.create()(class claim extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
            }
        }

        componentDidMount() {
            this.props.dispatch({
                type: 'deposit/getDepositsList',
                payload: {
                    status: "0",
                }
            });
        }

        handleEdit = (record, type) => {
            this.props.dispatch({type: 'deposit/getDepositById', payload: {id: record.id,forex_relate_id:record.forex_relate_id}});
            this.props.dispatch({type: 'deposit/getDepositReleaseList', payload: {id: record.id}});
            this.deposit.handleOpen(type);
        }



        handleSearch = () => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.deposit.page;
            newPage.page = 1;

            let beginTime = "";
            let endTime = "";

            if (values.rectime != null && values.rectime != undefined) {
                if(values.rectime.length >=2 ){
                    if(values.rectime[0] != ''){
                        beginTime = values.rectime[0].format(dateFormat);
                    }
                    if(values.rectime[1] != ''){
                        endTime = values.rectime[1].format(dateFormat);
                    }

                }
            }

            this.props.dispatch({
                type: 'deposit/getDepositsList',
                payload: {
                    time: values.time,
                    customerno: values.customerno,
                    depositNo: values.depositNo,
                    status: values.status,
                    beginTime: beginTime,
                    endTime: endTime,
                    page: newPage
                }
            });
        };



        columns = [
            {title: '客户编码', dataIndex: 'customerno', key: 'customerno', width: '150px', fixed: true},

            {title: '客户名称', dataIndex: 'companyname', key: 'companyname'},

            {title: '外汇单号', dataIndex: 'forexid', key: 'forexid',},

            {title: '保证金号', dataIndex: 'depositno', key: 'depositno'},

            {title: '订单编号', dataIndex: 'orderno', key: 'orderno',},

            {title: '保证金金额', dataIndex: 'amount', key: 'amount'},

            {title: '待释放金额', dataIndex: 'unpayamount', key: 'unpayamount'},

            {title: '操作', width: '100px', fixed: 'right', key: 'key_oprator',

                render: (text, record, index) => {

                    if(record.releasestatus == 0){
                        return (
                            <span>
                       <RightBox resource={RightContacts.CAPITAL.CAPITAL_PAY_RELEASE}><a href="#" onClick={this.handleEdit.bind(this, record, OperatorConstant.deposit_release)}>释放保证金</a></RightBox>
                    </span>
                        );
                    }else if(record.releasestatus == 1){
                        return (
                            <span>
                        <a href="#" onClick={this.handleEdit.bind(this, record, OperatorConstant.view)}>查看</a>
                    </span>
                        );
                    }
                }
            }
        ];

        render () {
            let dataSouce = R.isNil(this.props.deposit.dataSource) ? {} : this.props.deposit.dataSource;
            let formItemLayout = {labelCol: {span: 6}, wrapperCol: {span: 18}};
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();

            let beginTime = "";
            let endTime = "";
            if (values.rectime != null && values.rectime != undefined) {
                if(values.rectime.length >=2 ){
                    if(values.rectime[0] != ''){
                        beginTime = values.rectime[0].format(dateFormat);
                    }
                    if(values.rectime[1] != ''){
                        endTime = values.rectime[1].format(dateFormat);
                    }

                }
            }

            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                pageSize: this.props.deposit.page.size,
                total: this.props.deposit.page.totalRow,
                showSizeChanger: true,
                current:this.props.deposit.page.page,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.deposit.page;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'deposit/getDepositsList',
                        payload: {
                            time: values.time,
                            customerno: values.customerno,
                            depositNo: values.depositNo,
                            status: values.status,
                            beginTime: beginTime,
                            endTime: endTime,
                            page: newPage
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.deposit.page;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'deposit/getDepositsList',
                        payload: {
                            time: values.time,
                            customerno: values.customerno,
                            depositNo: values.depositNo,
                            status: values.status,
                            beginTime: beginTime,
                            endTime: endTime,
                            page: newPage
                        }
                    });
                }
            }
            return (
                <div>

                    <Form >
                        <div style={{width: 1250}}>
                            <Row >
                                <Col span={7}>
                                    <FormItem {...formItemLayout} label={'客户代码'}>
                                        {getFieldDecorator('customerno', {})(
                                            <Input style={{width: 200}}/>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={8}>
                                    <FormItem  {...formItemLayout} label={'收汇时间'}>
                                        {getFieldDecorator('rectime', {})(
                                            <RangePicker
                                                style={{width:'200px'}}
                                                showTime={{ format: 'YYYY-MM-DD' }}
                                                format="YYYY-MM-DD"
                                                placeholder={['开始时间', '结束时间']}
                                            />
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            {/*<Row >*/}
                                {/*<Col span={8}>*/}
                                    {/*<FormItem {...formItemLayout} label={'付汇账号'}>*/}
                                        {/*{getFieldDecorator('payaccount', {})(*/}
                                            {/*<Input style={{width: 200}}/>*/}
                                        {/*)}*/}
                                    {/*</FormItem>*/}
                                {/*</Col>*/}
                                {/*<Col span={8}>*/}
                                    {/*<FormItem {...formItemLayout} label={'保证金号'}>*/}
                                        {/*{getFieldDecorator('depositNo', {})(*/}
                                            {/*<Input style={{width: 200}}/>*/}
                                        {/*)}*/}
                                    {/*</FormItem>*/}
                                {/*</Col>*/}
                            {/*</Row>*/}

                            <Row>
                                <Col span={7}>
                                    <FormItem
                                        {...formItemLayout}
                                        label={'状态'}
                                    >
                                        {getFieldDecorator('status', {
                                            initialValue: "0"
                                        })(
                                            <RadioGroup style={{width: 800}} >
                                                <Radio value='0'>待释放</Radio>
                                                <Radio value='1'>已释放</Radio>
                                            </RadioGroup>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={8} style={{textAlign: 'left'}}>
                                    <Button type="primary" icon="search" style={{marginLeft: 50}} onClick={this.handleSearch.bind(this)}>查询</Button>
                                </Col>
                            </Row>
                        </div>
                    </Form>

                    <Table rowKey="id" dataSource={this.props.deposit.list} columns={this.columns} bordered={true}
                           size={'middle'} pagination={pagination}/>

                    <Deposit wrappedComponentRef={(inst) => this.deposit = inst} dispatch={this.props.dispatch}
                                 deposit = {this.props.deposit}
                                 handleSearch ={this.handleSearch}
                                 dataSource={this.props.deposit.dataSource}/>


                </div>
            )
        }
    })


)
